﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Demonstartor
{
    class AdditionalData
    {
        public string FlightXMLPath { get; private set; }
        public string FlightRKXMLPath { get; private set; }
        public double MinDesiredDensity { get; private set; }
        public double[] Density { get; private set; }
        public TimeSpan AdditionalTime { get; private set; }

        public AdditionalData(string FileName)
        {
            string SourcePath;
            if (File.Exists("sourcepath.txt"))
            {
                TextReader TR1 = new StreamReader("sourcepath.txt");
                SourcePath = TR1.ReadLine();
                TR1.Close();
            }
            else
                SourcePath = null;


            TextReader TR = new StreamReader(FileName);
            FlightXMLPath = TR.ReadLine();

            if (SourcePath != null)
            {
                string[] SPXMLPath = FlightXMLPath.Split('\\');
                int SPCount = SPXMLPath.Count();
                FlightXMLPath = SourcePath + '\\' + SPXMLPath[SPCount - 3] + '\\' + SPXMLPath[SPCount - 2] + '\\' + SPXMLPath[SPCount - 1];
            }

            string StrTmp = TR.ReadLine();
            double TmpD;
            if (double.TryParse(StrTmp.Replace(".", ","), out TmpD))
            {
                MinDesiredDensity = TmpD;
                FlightRKXMLPath = null;
            }
            else
            {
                FlightRKXMLPath = StrTmp;


                if (SourcePath != null)
                {
                    string[] SPXMLPath = FlightRKXMLPath.Split('\\');
                    int SPCount = SPXMLPath.Count();
                    FlightRKXMLPath = SourcePath + '\\' + SPXMLPath[SPCount - 3] + '\\' + SPXMLPath[SPCount - 2] + '\\' + SPXMLPath[SPCount - 1];
                }

                MinDesiredDensity = Convert.ToDouble(TR.ReadLine().Replace(".", ","));
            }

            List<double> TmpDensity = new List<double>();
            string Cur;
            Cur = TR.ReadLine();
            TimeSpan Tmp;

            if ((Cur != null) && TimeSpan.TryParse(Cur, out Tmp))
                AdditionalTime = Tmp;
            else
            {
                AdditionalTime = new TimeSpan(0);
                TmpDensity.Add(Convert.ToDouble(Cur.Replace(".", ",")));
            }

            while ((Cur = TR.ReadLine()) != null)
                TmpDensity.Add(Convert.ToDouble(Cur.Replace(".", ",")));
            Density = TmpDensity.ToArray();
        }

        public double[] GetDensity(int StartInd, int EndInd)
        {
            if (StartInd < 0)
                StartInd = 0;
            if (EndInd > Density.Count())
                EndInd = Density.Count();
            double[] Res = new double[EndInd - StartInd];
            for (int i = StartInd; i < EndInd; i++)
            {
                Res[i - StartInd] = Density[i];
            }

            return Res;
        }

        public double[] GetBorderLine(int StartInd, int EndInd)
        {
            if (StartInd < 0)
                StartInd = 0;
            if (EndInd > Density.Count())
                EndInd = Density.Count();

            double[] Res = new double[EndInd - StartInd];
            for (int i = StartInd; i < EndInd; i++)
            {
                Res[i - StartInd] = MinDesiredDensity;
            }

            return Res;
        }
    }
}
